package com.xiaoshuidi.cloud.module.rooms.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xiaoshuidi.cloud.module.rooms.framework.config.ConfigValues;
import com.xiaoshuidi.cloud.module.rooms.service.BaiDuMapService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class BaiDuMapServiceImpl implements BaiDuMapService {

    private final ConfigValues configValues;


    /**
     * 百度地图-地点输入提示
     *
     * @param address
     * @return
     */
    @Override
    public JSONArray locationEntryPrompt(String address, String coordType, String city) {
        String locationEntryPrompt = configValues.getLocationEntryPrompt().replace("*{region}",city);
        locationEntryPrompt = StrUtil.format(locationEntryPrompt, address, configValues.getBaiduAK(), coordType);
        String resp = HttpUtil.get(locationEntryPrompt);
        JSONObject jsonObject = JSONObject.parseObject(resp);
        if (!jsonObject.get("status").toString().equals("0")) {
            return null;
        }
        JSONArray result = (JSONArray) jsonObject.get("result");
        return result;
    }

    @Override
    public JSONObject latLonToAddress(String latLon, String coordType) {
        log.info("百度逆地理位置编码,请求经纬度:{}", latLon);
        String baiduLonLatToAddressUrl = configValues.getBaiduLonLatToAddressUrl();
        baiduLonLatToAddressUrl = StrUtil.format(baiduLonLatToAddressUrl, configValues.getBaiduAK(), coordType, latLon);
        String resp = HttpUtil.get(baiduLonLatToAddressUrl);
        log.info("百度逆地理位置编码,请求经纬度:{}, 百度响应:{}", latLon, resp);
        JSONObject jsonObject = JSONObject.parseObject(resp);
        if (!jsonObject.get("status").toString().equals("0")) {
            return null;
        }
        JSONObject result = (JSONObject) jsonObject.get("result");
        return result;
    }

    /**
     * 经纬度转换
     *
     * @param latLon
     * @return
     */
    @Override
    public String convertCoordinates(String latLon, int from, int to) {
        log.info("百度经纬度转换,请求经纬度:{}", latLon);
        String convertCoordinates = configValues.getConvertCoordinates();
        convertCoordinates = StrUtil.format(convertCoordinates, latLon, from, to, configValues.getBaiduAK());
        String resp = HttpUtil.get(convertCoordinates);
        log.info("百度经纬度转换,请求经纬度:{},百度响应:{}", latLon, resp);
        JSONObject jsonObject = JSONObject.parseObject(resp);
        if (!jsonObject.get("status").toString().equals("0")) {
            return null;
        }
        JSONArray jsonArray = (JSONArray) jsonObject.get("result");
        if (ObjectUtil.isEmpty(jsonArray)) {
            return null;
        }
        JSONObject lonlatObj = (JSONObject) jsonArray.get(0);
        String lng = lonlatObj.get("x").toString();
        String lat = lonlatObj.get("y").toString();
        log.info("百度经纬度转换,请求经纬度:{},转换后经纬度:{}", latLon, lat + "," + lng);
        return lat + "," + lng;
    }
}
